package component.security.encrypt;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.KeyPair;


import component.security.util.Constant;
import component.security.util.FileUtil;


/**
 * 获取密钥对运行类
 *
 */
public class KeyPairRunner {
	
	/**
	 * 将密钥对写入文件
	 * @preserve
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception{		
		createKeyPair();
	}
	
	
	/**
	 * 生成公钥密钥对
	 * @throws Exception
	 */
	private static void createKeyPair()throws Exception{
//		密码判断
		String pass = "g79r97p313";

		System.out.println("请输入密码：\n");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   
	    int ii=1;
	    while (ii<4){
	    	String pwd = br.readLine();
		    if (!pass.equals(pwd) ){
		    	System.out.println("对不起，您输入的密码错误，请重新输入！\n");
		    	ii++;	    
		    }  else {
		    	break;
		    }
	    }
	    
	    if (ii==4) {
	    	System.out.println("对不起，您输入密码错误三次，请和管理员联系！\n");
	    	return ;
	    }
		String priKeyFile = FileUtil.getFileDir(Constant.PRIKEY_FILE) ;
		String pubkeyFile = FileUtil.getFileDir(Constant.PUBKEY_FILE) ;
		
        int keysize = 1024;
          
        String seed = String.valueOf(System.currentTimeMillis());
        
        if (seed==null || seed.trim().equals("")) seed = "GEVER";        
		//获取密钥对
        KeyPair keyPair= KeyUtil.generateKeyPair(KeyUtil.RSA, seed, keysize);
        
        System.out.println("开始生成对称密码.........\n");
        
		//存储密钥对文件
		Util.saveKeyPair(keyPair, priKeyFile, pubkeyFile);		
		
		System.out.println("生成对称成功！\n");
		
		br.readLine();
		
	}
}
